<template>
  <div style="padding: 20px;">
    <h2 style="margin-bottom: 20px;">股票详情</h2>
    <el-descriptions :column="2" border v-loading="loading">
      <el-descriptions-item label="股票代码">
        {{ stockDetail.code }}
      </el-descriptions-item>
      <el-descriptions-item label="股票名称">
        {{ stockDetail.name }}
      </el-descriptions-item>
      <el-descriptions-item label="公司网址" :span="2">
        <a :href="stockDetail.url" target="_blank" v-if="stockDetail.url">
          {{ stockDetail.url }}
        </a>
        <span v-else>-</span>
      </el-descriptions-item>
    </el-descriptions>
    <div style="margin-top: 20px;">
      <el-button type="primary" @click="handleEdit">编辑</el-button>
      <el-button @click="handleBack">返回</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'StockView',
  data() {
    return {
      stockDetail: {
        id: '',
        code: '',
        name: '',
        url: ''
      },
      loading: false
    }
  },
  created() {
    const id = this.$route.query.id;
    if (id) {
      this.loadStockDetail(id);
    } else {
      this.$message.warning('缺少股票ID参数');
      this.$router.push({ name: 'Stock' });
    }
  },
  methods: {
    loadStockDetail(id) {
      console.log('loadStockDetail', id);
      this.loading = true;
      this.request.get("/stock/" + id).then(res => {
        this.loading = false;
        if (res.code === 200) {
          this.stockDetail = res.data;
        }
      }).catch(() => {
        this.loading = false;
        this.$message.error('加载数据失败');
      });
    },
    handleEdit() {
      this.$router.push({
        name: 'StockForm',
        query: { id: this.stockDetail.id }
      });
    },
    handleBack() {
      this.$router.push({ name: 'Stock' });
    }
  }
}
</script>

<style scoped>
</style>